/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import controller.Forms.EnterpriseForm;
import dao.EnterpriseDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Enterprise;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class UpDateEnterpriseAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {

            User user = (User) request.getSession().getAttribute("User");

            if (user == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            } else {

                Enterprise e = new Enterprise();
                EnterpriseForm eForm = (EnterpriseForm) form;

                e.setTxtCorporateName(eForm.getTxtCorporateName());
                e.setTxtCnpj(eForm.getTxtCnpj());
                e.setTxtStateRegistration(eForm.getTxtStateRegistration());
                e.setTxtMunicipalRegistration(eForm.getTxtMunicipalRegistration());
                e.setTxtZipcode(eForm.getTxtZipCode());
                e.setTxtAddress(eForm.getTxtAddress());
                e.setTxtNumero(eForm.getTxtNumero());
                e.setTxtComplement(eForm.getTxtComplement());
                e.setTxtDistrict(eForm.getTxtDistrict());
                e.setTxtCity(eForm.getTxtCity());
                e.setTxtLstUF(eForm.getLstUF());
                e.setTxtDdd(eForm.getTxtDdd());
                e.setTxtPhone(eForm.getTxtPhone());
                e.setTxtEmail(eForm.getTxtEmail());
                e.setTxtJointStock(eForm.getTxtJoinStock());
                e.setTxtLstStatus(eForm.getLstStatus());
                e.setTxtId(eForm.getTxtId());

                EnterpriseDAO userDAO = new EnterpriseDAO();
                userDAO.update(e);
                userDAO.closeDAO();

                return mapping.findForward("homeAlias");
            }

        } catch (Exception e) {

            System.out.println("CADASTRARUSUARIOCATION - " + Erro.erroMsg[Erro.DADOS_INVÁLIDOS] + e.getMessage());
            return mapping.findForward("erroAlias");
        }

    }
}
